export default {
    getCondition(fIndex, oneFunnel) { // 添加限制条件，如果该事件没有先拉取
        const { biz, logType, event, feCreateType } = oneFunnel
        return new Promise((resolve, reject) => {
            // 私有限制条件为空，且不是预置事件 或 页面组，就拉取私有限制条件
            try {
                this.$set(this.extraParamsList.parameterList, fIndex, {})
                if (logType !== 'pages' && logType.indexOf('presetEvent') === -1) {
                    this.$api.analysis[`${logType}ParameterList`]({
                        biz: logType === 'server' && feCreateType === 0 ? '' : biz,
                        event: event,
                        terminal: this.customType[logType],
                        platform: logType === 'server' && feCreateType === 0 ? 'common' : '',
                        pageNum: -1
                    }).then(res => {
                        if (res.ret === 0) {
                            res.content.forEach(element => {
                                this.$set(this.extraParamsList.parameterList[fIndex], element.parameterName || element.parameter_name, element)
                            })
                            resolve()
                        } else {
                            reject('获取事件属性失败，请联系后台管理员')
                        }
                    })
                } else if ((logType === 'presetEvents' || logType === 'presetEventsApp')) {
                    this.$api.analysis.findPresetList({
                        biz,
                        appTerminal: logType,
                        operation: event
                    }).then(res => {
                        if (res.code === 0) {
                            res.data.forEach(item => {
                                item.description = item.name
                                this.$set(this.extraParamsList.parameterList[fIndex], item.name, item)
                            })
                            resolve()
                        }
                    })
                } else {
                    resolve('无需拉取')
                }
            } catch (e) {
                reject(e)
            }
        })
    },
    getGlobalCondition(funnelId) { // 获取 全局 基础选项
        return new Promise((resolve, reject) => {
            this.$api.analysis.getGlobalCondition({
                funnelId: funnelId,
                type: this.conditionMode
            }).then(res => {
                if (res.code === 0) {
                    const { bizList, expressions, expressionsNumber, comBizList } = res.data
                    this.bizList = Object.assign(...bizList)
                    this.comBizList = Object.assign(...comBizList)
                    this.comBizKeyList = Object.keys(this.comBizList)
                    this.operatorList = expressions
                    this.numberOperatorList = expressionsNumber
                    this.comBizKey = Object.keys(this.comBizList)
                    this.getComBizEvent()
                    resolve(res)
                } else {
                    this.$message.error('获取基础选项失败，请联系后台管理员')
                    reject('获取基础选项失败，请联系后台管理员')
                }
            })
        })
    },
    setCommonPropertiesList(data) { // 初始化 公共限制条件列表 commonPropertiesList
        const commonPropertiesList = {
            auto: data.h5AutoCommonProperties,
            pages: data.h5PagesCommonProperties,
            presetEvents: data.h5PresetCommonProperties, // h5 预置事件 公共属性
            customH5: data.h5CustomCommonProperties,
            customApp: data.appCustomCommonProperties,
            presetEventsApp: data.appPresetCommonProperties, // App 预置事件 公共属性
            server: data.serverCommonProperties,
            customWeapp: data.weappCustomCommonProperties,
            presetEventsWeapp: data.weappPresetCommonProperties, // Weapp 预置事件 公共属性
            presetEventsServer: data.serverPresetCommonProperties || [], // Server 预置事件 公共属性
            presetEventsAd: data.adPresetCommonProperties
        }
        for (let type in commonPropertiesList) { // 将parameter_name的值作为key，item作为value，组合为k-v形式
            let properties = {}
            if (!commonPropertiesList[type]) continue
            commonPropertiesList[type].forEach(item => {
                properties[item.parameter_name] = item
            })
            commonPropertiesList[type] = properties
        }
        this.commonPropertiesList = commonPropertiesList
    },

}